package com.yida.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.utils.regular.ReUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.system.api.domain.SysSms;
import com.yida.system.api.domain.SysUser;
import com.yida.system.service.ISysSmsService;
import com.yida.system.service.ISysUserService;

/**
 * 手机短信记录Controller
 * 
 * @author ruoyi
 * @date 2022-10-26
 */
@RestController
@RequestMapping("/sms")
public class SysSmsController extends BaseController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysSmsService sysSmsService;

    /**
     * 查询手机短信记录列表
     */
    @RequiresPermissions("system:sms:list")
    @GetMapping("/list")
    public TableDataInfo list(SysSms sysSms) {
        startPage();
        List<SysSms> list = sysSmsService.selectSysSmsList(sysSms);
        return getDataTable(list);
    }

    /**
     * 导出手机短信记录列表
     */
    @RequiresPermissions("system:sms:export")
    @Log(title = "手机短信记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSms sysSms) {
        List<SysSms> list = sysSmsService.selectSysSmsList(sysSms);
        ExcelUtil<SysSms> util = new ExcelUtil<SysSms>(SysSms.class);
        util.exportExcel(response, list, "手机短信记录数据");
    }

    /**
     * 获取手机短信记录详细信息
     */
    @RequiresPermissions("system:sms:query")
    @GetMapping(value = "/{smsId}")
    public AjaxResult getInfo(@PathVariable("smsId") Long smsId) {
        return AjaxResult.success(sysSmsService.selectSysSmsBySmsId(smsId));
    }

    /**
     * 新增手机短信记录
     */
    @RequiresPermissions("system:sms:add")
    @Log(title = "手机短信记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSms sysSms) {
        return toAjax(sysSmsService.insertSysSms(sysSms));
    }

    /**
     * 修改手机短信记录
     */
    @RequiresPermissions("system:sms:edit")
    @Log(title = "手机短信记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSms sysSms) {
        return toAjax(sysSmsService.updateSysSms(sysSms));
    }

    /**
     * 删除手机短信记录
     */
    @RequiresPermissions("system:sms:remove")
    @Log(title = "手机短信记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{smsIds}")
    public AjaxResult remove(@PathVariable Long[] smsIds) {
        return toAjax(sysSmsService.deleteSysSmsBySmsIds(smsIds));
    }

    /**
     * 清除手机短信
     * 
     * @return
     */
    @RequiresPermissions("system:sms:remove")
    @Log(title = "手机短信记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        sysSmsService.cleanSmsInfo();
        return AjaxResult.success();
    }

    /**
     * 发送验证码
     */
    @InnerAuth
    @PostMapping("/sendSmsCode")
    public R<SysSms> sendSmsCode(@RequestBody SysUser sysUser) {
        if ("admin".equals(sysUser.getUserName())) {
            return R.fail("系统管理员admin不在校验范围内！");
        }
        // 校验非管理员用户
        String phonenumber = sysUser.getPhonenumber();
        SysUser sysUserVo = userService.selectUserByPhonenumber(phonenumber);
        if (sysUserVo == null) {
            return R.fail("当前系统不存在该手机号用户！");
        }
        if (!sysUserVo.getUserName().equals(sysUser.getUserName())) {
            return R.fail("当前系统该手机号用户名与输入不一致，请核对后输入！");
        }
        // 手机号能查到用户名，且一致则进行发送
        SysSms sysSms = sysSmsService.sendSms(phonenumber);
        return R.ok(sysSms);
    }

    /**
     * 20230321
     * 
     * hmh
     * 
     * 通用发送验证码
     * 
     * 20230404修定判断
     */
    @InnerAuth
    @PostMapping("/sendSmsByPhonenumber")
    public R<SysSms> sendSmsByPhonenumber(@RequestBody SysSms sysSms) {
//        String phonenumber = sysSms.getPhonenumber();
//        if (StringUtils.isEmpty(phonenumber)) {
//            return R.fail("发送短信手机号为空，发送失败！");
//        }
//        // 手机号不符合规定
//        if (!ReUtils.checkPhone(phonenumber)) {
//            R.fail("输入手机号[" + phonenumber + "]不合法");
//        }
//        if (sysSms.getSmsType() != 1 && StringUtils.isEmpty(sysSms.getSms())) {
//            return R.fail("发送短信内容不能为空，发送失败！");
//        }
        SysSms rSysSms = sysSmsService.sendInnerSms(sysSms);
        return R.ok(rSysSms);
    }

    /**
     * 20230404
     * 
     * hmh
     * 
     * 通用发送验证码
     */
    @InnerAuth
    @PostMapping("/sendAuthSmsByPhonenumber")
    public R<SysSms> sendAuthSmsByPhonenumber(@RequestBody SysSms sysSms) {
        if (StringUtils.isEmpty(sysSms.getPhonenumber())) {
            return R.fail("发送短信手机号为空，发送失败！");
        }
        if (sysSms.getSmsType() != 1 && StringUtils.isEmpty(sysSms.getSms())) {
            return R.fail("发送短信内容不能为空，发送失败！");
        }
        SysSms rSysSms = sysSmsService.sendSms(sysSms);
        return R.ok(rSysSms);
    }

    /**
     * 删除验证码记录
     */
    @InnerAuth
    @GetMapping("/removeSmsCode/{phonenumber}")
    public R<Boolean> removeSmsCode(@PathVariable String phonenumber) {
        sysSmsService.deleteSysSmsByPhonenumber(phonenumber, 1);
        return R.ok(null, "删除成功");
    }

    /**
     * 删除短信记录
     */
    @InnerAuth
    @GetMapping("/removeSms/")
    public R<Boolean> removeSms(@RequestParam("phonenumber") String phonenumber, @RequestParam("smsType") Integer smsType, @RequestParam("smsSource") String smsSource) {
        sysSmsService.deleteSysSmsByPhonenumber(phonenumber, 1);
        return R.ok(null, "删除成功");
    }

}
